﻿using System.Data;
using System.Linq;
using System.Web.Security;
using System.Web.Mvc;
using GameSchoolApplication.Models;
using GameSchoolApplication.Repository;
using GameSchoolApplication.Interfaces;

namespace GameSchoolApplication.Controllers
{ 
    public class PersonController : Controller
    {
        private IPersonRepository _person;

        // GET: /Person/
        public PersonController()
        {
            _person = new PersonRepository();
        }

		public PersonController(IPersonRepository rep)
		{
		    _person = rep;
		}

		/// <summary>
		/// Gets all persons and order them by Group 
		/// </summary>
		/// <returns></returns>
		[HttpGet]
        public ViewResult Index()
        {
            return View(_person.GetAllPersons().OrderBy(m => m.Group));
        }
														 
		// GET: /Person/Details/5
        public ViewResult Details(int? id)
        {
        	if (id.HasValue)
        	{
        		var personFound = _person.GetPersonById(id);
				if (personFound != null)
				{
					return View(personFound);
				}
        	}
        	return View("NotFound");
        }

        // GET: /Person/Create
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Person/Create
        [HttpPost]
        public ActionResult Create(Person person)
        {
            if (ModelState.IsValid)
            {
            	var exist = _person.GetAllPersons().Where(m => m.UserName == person.UserName).SingleOrDefault();
				if (exist == null)
				{
					_person.CreatePerson(person);
					return RedirectToAction("Index");
				}

            }
            ViewBag.Message = "Person with this username already exists in the database";
            return View(person);
        }

        // GET: /Person/Edit/5
        [HttpGet]
		public ActionResult Edit(int? id)
		{
            // ATH. annaðhvort bæta hér við account uppfæringu eða færa notnafn, lykilorð, hóp úr _Form í Create
			if (id.HasValue)
			{
				var personFound = _person.GetPersonById(id);
				if (personFound != null)
				{
					return View(personFound);
				}
			}
			return View("NotFound");
		}

        // POST: /Person/Edit/5
        [HttpPost]
        public ActionResult Edit(Person person)
        {
            if (ModelState.IsValid)
            {
            	_person.UpdatePerson(person);
                return RedirectToAction("Index");
            }
            return View(person);
        }

        // GET: /Person/Delete/5
        public ActionResult Delete(int? id)
        {
            if(id.HasValue)
            {
            	var personToDelete = _person.GetPersonById(id);
				if(personToDelete != null)
				{
					return View(personToDelete);
				}
            }
        	return View("NotFound");
        }

        // POST: /Person/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int? id)
        {            
            if(id.HasValue)
            {
            	var personToDelete = _person.GetPersonById(id);
				if(personToDelete != null)
				{
					_person.DeletePerson(personToDelete);
				}
            }
            return RedirectToAction("Index");
        }

		[HttpGet]
		public ActionResult NotFound()
		{
			return View("NotFound");
		}

		protected override void Dispose(bool disposing)
		{
			_person.Dispose();
			base.Dispose(disposing);
		}
    }
}